import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data

#下面为mnist训练集
mnist=input_data.read_data_sets("./2RNN/data",one_hot=True)
# batch_x,batch_y=mnist.train.next_batch(2)
# x= batch_x



# print(x.shape)
with tf.Session() as sess:
    #加载模型文件
    saver = tf.train.import_meta_graph('models/cpk.meta')
    #恢复参数，依赖于session,dir表示模型保存的目录路径，此时所有张量的值都在session中
    saver.restore(sess,tf.train.latest_checkpoint('models'))
    graph = tf.get_default_graph()
 
    #恢复传入值
    xx = graph.get_tensor_by_name('x_input:0')
 
    #计算利用训练好的模型参数计算预测值
    preds = graph.get_tensor_by_name('y_yat:0')
    print(preds.shape)
    # print('predict values:%s' % sess.run(preds,feed_dict={xx:input_x}))
    # print(len(input_y))
    for i in range(100):
        #下面为mnist测试集
        input_x,input_y = mnist.test.next_batch(100)
        output = sess.run(preds,feed_dict={xx:input_x})
        s = 0
        for i in range(len(output)):
            if np.argmax(output[i]) == np.argmax(input_y[i]):
                s += 1
            # print(np.argmax(i))
        print('当前批次正确率为%d/%d' % (s,len(output)))